﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Listener.Model;

public partial class BasicBase_Listenner_listenInput : System.Web.UI.Page
{
    bool bl = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();

        }
     
        Bind2();
        Bind3();


    }
    private void Bind()
    {
        string Dept = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Dept == null || Dept.Length < 5)
            return;

        
        
        string strSql = "";
        if (Dept.Substring(0, 4).Equals("1300"))
            strSql += " 1=1";
        else
            strSql += " Left(DepartmentID,4)='" + Dept.Substring(0,4) + "'";

        //this.DptDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(strSql);
        this.DptDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(strSql);
        this.DptDepartment.DataTextField = "FullName";
        this.DptDepartment.DataValueField = "DepartmentID";
        this.DptDepartment.DataBind();
        DptDepartment.Items.Insert(0, new ListItem("请选择单位", "00"));
        DptDepartment.SelectedValue = Request["DepartmentID"];
        if (Request["DepartmentID"] != null)
        {
            this.dptDuty.DataSource = Yipusoft.Account.BLL.Department.GetList("Left(DepartmentID,4)='" + Request["DepartmentID"].Substring(0, 4) + "'");
            this.dptDuty.DataTextField = "FullName";
            this.dptDuty.DataValueField = "DepartmentID";
            this.dptDuty.DataBind();
            dptDuty.SelectedValue = DptDepartment.SelectedValue;
        }
        Promt.Text = "";
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
	try
        {
        //Bind2();
            Promt.Text = "";
        if (DptDepartment.SelectedValue.Equals("00") || string.IsNullOrEmpty(DptDepartment.SelectedValue))
        {
            Promt.Text = "请选择单位";
            return;
        }
        int Flag = 0;
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(3, 4);
        for (int i = 0; i < TableList.Rows.Count; i++)
        {
            for (int n = 0; n < TableList.Rows[i].Cells.Count; n++)
            {
                if (TableList.Rows[i].Cells[n].HasControls())
                {
                    if (TableList.Rows[i].Cells[n].Controls[0] is CheckBox)
                    {
                        CheckBox Box = TableList.Rows[i].Cells[n].Controls[0] as CheckBox;
                        if (bool.Parse(TableList.Rows[i].Cells[n].Attributes["Checked"].ToString()) != Box.Checked)
                        {
                            if (Box.Checked)
                            {
                                Yipusoft.Listener.Model.CallUser CUser = new Yipusoft.Listener.Model.CallUser();
                                CUser.LastLoginTime = DateTime.Now;
                                CUser.LoginID = DepartmentID + Yipusoft.Listener.BLL.CallUser.GetID();
                                CUser.Password = txtPwd.Text.Trim();
                                CUser.State = "";
                                CUser.UserID = TableList.Rows[i].Cells[n].Attributes["UserID"].ToString();
                                CUser.DepartmentID = dptDuty.SelectedValue;
                                CUser.DepartmentName = dptDuty.SelectedItem.Text;
                                CUser.UserName = Box.Text.Trim();
                                CUser.UserType = "";
                                Flag = Yipusoft.Listener.BLL.CallUser.Add(CUser);
                            }
                            else
                            {
                                if (TableList.Rows[i].Cells[n].Attributes["UserID"] != null)
                                    Flag = Yipusoft.Listener.BLL.CallUser.Delete(TableList.Rows[i].Cells[n].Attributes["UserID"].ToString());
                            }
                        }
                    }
                }
            }
        }
	
        if (Flag > 0)
            Promt.Text = "操作成功";

        bl = false;
        AddCadre();
	}
        catch { Promt.Text = "操作失败"; };
    }

    private int AddCadre()
    {
        //Bind2();
        if (DptDepartment.SelectedValue.Equals("00"))
            return 0;
        int Flag = 0;
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(3, 4);
        for (int i = 0; i < TableList2.Rows.Count; i++)
        {
            for (int n = 0; n < TableList2.Rows[i].Cells.Count; n++)
            {
                if (TableList2.Rows[i].Cells[n].HasControls())
                {
                    if (TableList2.Rows[i].Cells[n].Controls[0] is CheckBox)
                    {
                        CheckBox Box = TableList2.Rows[i].Cells[n].Controls[0] as CheckBox;
                        if (bool.Parse(TableList2.Rows[i].Cells[n].Attributes["Checked"].ToString()) != Box.Checked)
                        {
                            if (Box.Checked)
                            {
                                Yipusoft.Listener.Model.CallUser CUser = new Yipusoft.Listener.Model.CallUser();
                                CUser.LastLoginTime = DateTime.Now;
                                CUser.LoginID = DepartmentID + Yipusoft.Listener.BLL.CallUser.GetID();
                                CUser.Password = txtPwd.Text.Trim();
                                CUser.State = "";
                                CUser.UserID = TableList2.Rows[i].Cells[n].Attributes["UserID"].ToString();
                                CUser.DepartmentID = dptDuty.SelectedValue;
                                CUser.DepartmentName = dptDuty.SelectedItem.Text;
                                CUser.UserName = Box.Text.Trim();
                                CUser.UserType = "";
                                Flag = Yipusoft.Listener.BLL.CallUser.Add(CUser);
                            }
                            else
                            {
                                if (TableList2.Rows[i].Cells[n].Attributes["UserID"] != null)
                                    Flag = Yipusoft.Listener.BLL.CallUser.Delete(TableList2.Rows[i].Cells[n].Attributes["UserID"].ToString());
                            }
                        }
                    }
                }
            }
        }

        if (Flag > 0)
            Promt.Text = "操作成功";

        return Flag;
        
    }
    protected void DptDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DptDepartment.SelectedValue.Equals("00"))
            return;
        Response.Redirect("ListenInput.aspx?DepartmentID=" + DptDepartment.SelectedValue + "");
        //TableList.Rows.Clear();
        //Bind2();

    }
    private void Bind2()
    {
        Promt.Text = "";
        string DepartmentID = DptDepartment.SelectedValue;
        if (bl)
            return;

        Yipusoft.SoldierArchives.BLL.Soldier soldier = new Yipusoft.SoldierArchives.BLL.Soldier();
        string Where = " DepartmentID='" + DptDepartment.SelectedValue + "' And isDelete=0";
        List<Yipusoft.Listener.Model.CallUser> listuser = Yipusoft.Listener.BLL.CallUser.GetList(" DepartmentID='" + DepartmentID + "'");
        int Count = soldier.GetSoldiersCountByWhere(Where);
        List<Yipusoft.SoldierArchives.Model.SoldierArchiveInfo> list = soldier.GetSoldiersByWhere(Where, "SoldierID", "Desc", Count, 0);
        if (list == null)
            return;
        int index = 0;
        int Count1 = list.Count % 5 > 0 ? list.Count / 5 + 1 : list.Count / 5;
        for (int n = 0; n < Count1; n++)
        {
            TableRow Trw = new TableRow();
            TableCell Cell = null;
            TableList.Rows.Add(Trw);
            for (int i = 0; i < 6; i++)
            {
                if (index < list.Count)
                {
                    Cell = new TableCell();
                    CheckBox Box = new CheckBox();
                    Box.Text = list[index].SoldierName;
                    Box.Checked = listuser.Where(o => o.UserID.ToString() == list[index].SoldierID.ToString()).Count() > 0 ? true : false;
                    Cell.Controls.Add(Box);
                    Cell.Attributes["UserID"] = list[index].SoldierID.ToString();
                    Cell.Attributes["Checked"] = Box.Checked.ToString();
                    Cell.Width = 80;
                    //Cell.CssClass = "tabstyle";
                    Trw.Cells.Add(Cell);

                }
                else
                {
                    Cell = new TableCell();
                    // Cell.CssClass = "tabstyle";
                    Cell.Width = 80;
                    Trw.Cells.Add(Cell);

                }
                index++;

            }

        }
        bl = true;


    }

    private void Bind3()
    {
        Promt.Text = "";
        string strSql = "";
        string DepartmentID = DptDepartment.SelectedValue;
        

       
        string Where = " DepartmentID='" + DptDepartment.SelectedValue + "'";
        List<Yipusoft.Listener.Model.CallUser> listuser = Yipusoft.Listener.BLL.CallUser.GetList(" DepartmentID='" + DepartmentID + "'");

        List<DutyCadreInfo> list = Yipusoft.Listener.BLL.DutyCadre.GetList(Where);
        if (list == null)
            return;
        int Count = list.Count;
     
        int index = 0;
        int Count1 = list.Count % 5 > 0 ? list.Count / 5 + 1 : list.Count / 5;
        for (int n = 0; n < Count1; n++)
        {
            TableRow Trw = new TableRow();
            TableCell Cell = null;
            TableList2.Rows.Add(Trw);
            for (int i = 0; i < 6; i++)
            {
                if (index < list.Count)
                {
                    Cell = new TableCell();
                    CheckBox Box = new CheckBox();
                    Box.Text = list[index].UserName;
                    Box.Checked = listuser.Where(o => o.UserID.ToString() == list[index].UserID.ToString()).Count() > 0 ? true : false;
                    Cell.Controls.Add(Box);
                    Cell.Attributes["UserID"] = list[index].UserID.ToString();
                    Cell.Attributes["Checked"] = Box.Checked.ToString();
                    Cell.Width = 80;
                    //Cell.CssClass = "tabstyle";
                    Trw.Cells.Add(Cell);

                }
                else
                {
                    Cell = new TableCell();
                    // Cell.CssClass = "tabstyle";
                    Cell.Width = 80;
                    Trw.Cells.Add(Cell);

                }
                index++;

            }

        }
        bl = true;


    }
}
